<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php

class CreateFolder extends CApiComponent {
    public $_path;     public $cid;       public $cname;     public $aid;       
        private $_action = 0;

    
    public function __construct() {
        parent::init ();
        $this->result = array ();
        $this->result["state"] = false;
        $this->result["code"]  = 0;
        $this->result["message"] = "创建文件夹失败";
    }
    
    public function invoke($isParentId = true) {
                $device = UserDevice::model()->find("user_id={$this->_userId} and user_device_type=1");
        $this->_deviceId = $device["id"];
                                $this->handleSpace();
        
        if ($this->_parentId == 0) {
            $this->_path = "/{$this->_userId}/{$this->cname}";
        } elseif ($isParentId) {
            $parent = UserFile::model ()->findByPk ( $this->_parentId );
            if (empty ( $parent )) {
                $this->handleResult ( false, 3, "父目录不存在" );
                return;
            }
            
            if ($parent["file_type"] != 1) {
                $this->handleResult(false, 3, "父目录不存在");
                return ;
            }
            
            $this->_path = $parent["file_path"] . "/" . $this->cname;
        } else {
            $this->_path = "/{$this->_userId}/{$this->_path}";
        }
                                if (CUtils::checkNameInvalid($this->_path) != 0) {
             $this->result["msg"] = "命名不能包含下列字符串： ^|?*\\<\":>";
             return;
        }
                                $file = UserFile::model()->find(array('condition' => 'file_path=:file_path', 'params' => array(':file_path'=>$this->_path)));
        if ($file) {
            if ($file["is_deleted"] == 0) {
                $this->result["message"] = "文件夹已经存在";
                return ;
            }
        }
        $this->_path = CUtils::convertStandardPath($this->_path);
        $this->cid = $this->handleCreateByPath($this->_path);
        $this->result["state"] = true;
        $this->result["code"]  = 0;
        $this->result["message"] = "创建文件夹成功";
        $this->result["cname"] = $this->cname;
        $this->result['aid']   = $this->aid;
        $this->result['cid']   = $this->cid;
    }
    
    
    public function handleCreateByPath($path) {
        if ($path == "/{$this->_userId}" || $path == "\\" || $path == "/{$this->_userId}/") {
            return 0;
        }
        
        $event_uuid = CUtils::random_string(46);
        $file = UserFile::model()->find(array('condition' => 'file_path=:file_path', 'params' => array(':file_path'=>$path)));
        if (empty($file) || is_null($file)) {
            $pathInfo  = CUtils::pathinfo_utf($path);
            $parenPath = $pathInfo["dirname"];
            $fileName  = $pathInfo["basename"];
            $parentId  = $this->handleCreateByPath($parenPath);
            $file      = new UserFile();
            $file['user_id']          = $this->_userId;
            $file['file_type']        = 1;
            $file['parent_file_id']   = $parentId;
            $file['file_create_time'] = time();
            $file['file_update_time'] = time();
            $file['file_name']        = $fileName;
            $file['file_path']        = $path;
            $file['is_deleted']       = 0;
            $file['version_id']       = 0;
            $file['file_size']        = 0;
            $file["event_uuid"]       = $event_uuid;
            $file->save();
                        $event = new UserEvent();
            $event->CreateEvent($this->_userId, $this->_deviceId, $this->_action, $path, $path, $event_uuid);
        } else {
            if ($file["is_deleted"] == 1) {
                $file["is_deleted"]       = 0;
                $file["file_update_time"] = time();
                $file["event_uuid"]       = $event_uuid;
                $file->save();
                $event = new UserEvent();
                $event->CreateEvent($this->_userId, $this->_deviceId, $this->_action, $path, $path, $event_uuid);
            }
        }
        
        return $file["id"];
    }
    
    
    
    public function handleException($exception) {
        echo CJSON::encode ( $this->result );
    }

}
?>